App({
  onLaunch: function () {},
  globalData: {
    //用户信息
    userinfo: null,
    //域名
    baseurl: "https://dou.weiguo.antsot.com",
  },
  //获取组件主题
  getThemeConfig() {
    return {
      borderRadius: "8rpx",
      backgroundColor: "#FE2C55",
      fontColor: "#ffffff",
    };
  },
  //获取手机号
  getPhoneNumber({ params, success, fail }: any) {
    let that = this;
    const { iv, encryptedData } = params;
    new Promise((resolve: any, reject: any) => {
      if (iv && encryptedData && that.globalData.userinfo.session_key) {
        tt.request({
          url: that.globalData.baseurl + "/douyin/decrypt",
          data: {
            session_key: that.globalData.userinfo.session_key,
            encryptedData: encryptedData,
            iv: iv,
          },
          header: {
            "content-type": "application/json",
          },
          method: "POST",
          responseType: "text",
          success(res: any) {
            if (res.data.code === 0) {
              resolve(res.data.data);
            } else {
              reject("请求报错");
            }
          },
          fail(err: any) {
            reject(err.errMsg);
          },
        });
      } else {
        reject("参数错误");
      }
    })
      .then((res: any) => {
        // 回调前端模板
        success({
          phoneNumber: res.phoneNumber,
        });
      })
      .catch((err: any) => {
        tt.showToast({
          title: err,
          icon: "fail",
          duration: 2000,
        });
      });
    //需要刷新session_key
    // let resolt = that.Login();
    // resolt
    //   .then((session_key: any) => {
    //     new Promise((resolve: any, reject: any) => {
    //       if (iv && encryptedData && session_key) {
    //         tt.request({
    //           url: that.globalData.baseurl + "/douyin/decrypt",
    //           data: {
    //             session_key: session_key,
    //             encryptedData: encryptedData,
    //             iv: iv,
    //           },
    //           header: {
    //             "content-type": "application/json",
    //           },
    //           method: "POST",
    //           responseType: "text",
    //           success(res: any) {
    //             if (res.data.code === 0) {
    //               resolve(res.data.data);
    //             } else {
    //               reject("请求报错");
    //             }
    //           },
    //           fail(err: any) {
    //             reject(err.errMsg);
    //           },
    //         });
    //       } else {
    //         reject("参数错误");
    //       }
    //     })
    //       .then((res: any) => {
    //         // 回调前端模板
    //         success({
    //           phoneNumber: res.phoneNumber,
    //         });
    //       })
    //       .catch((err: any) => {
    //         tt.showToast({
    //           title: err,
    //           icon: "fail",
    //           duration: 2000,
    //         });
    //       });
    //   })
    //   .catch((err: any) => {
    //     console.log(err);
    //   });
  },
  //登录
  Login() {
    let that = this;
    return new Promise((resolve: any, reject: any) => {
      tt.login({
        force: true,
        success(res) {
          let { code } = res;
          tt.request({
            url: that.globalData.baseurl + "/douyin/code2session",
            data: {
              code: code,
            },
            header: {
              "content-type": "application/json",
            },
            method: "GET",
            responseType: "text",
            success(res: any) {
              if (res.data.code === 0) {
                resolve(res.data.data.session_key);
              } else {
                reject("请求报错");
              }
            },
            fail(err: any) {
              reject(err.errMsg);
            },
          });
        },
        fail() {
          reject("登陆失败");
        },
      });
    });
  },
});
